package com.example.administrator.baikangxing.Dao;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.bean.ContactUser;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sushil_Shen on 2017/3/28/0028.
 * Contact: Sushil_shen@163.com
 */

public class GetContact {
    private static int[] pics = {R.drawable.clothes, R.drawable.doctor, R.drawable.drug, R.drawable.food, R.drawable.health, R.drawable.info, R.drawable.person_info};
    private static ArrayList<ContactUser> list;

    public static ArrayList<ContactUser> getPhoneNumberFromMobile(Context context) {
        // TODO Auto-generated constructor stub
        list = new ArrayList<ContactUser>();
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, null);
        //moveToNext方法返回的是一个boolean类型的数据
        int index = 0;
        while (cursor.moveToNext()) {
            //读取通讯录的姓名
            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            //读取通讯录的号码
            String number = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            int flag = index % 7;
            ContactUser contactUser = new ContactUser(name, number, pics[flag]);
            list.add(contactUser);
            index++;
        }
        return list;
    }
}
